Skip to content

perf: BTreeMap::range() and incremental memory tracking in reassembly#24

Merged
Zious11 merged 8 commits intodevelopfrom
feature/reassembly-perf
Apr 7, 2026
Merged

perf: BTreeMap::range() and incremental memory tracking in reassembly#24
Zious11 merged 8 commits intodevelopfrom
feature/reassembly-perf

Conversation

@Zious11
Copy link
Copy Markdown
Owner

@Zious11 Zious11 commented Apr 7, 2026

Summary

  • Replace O(n) overlap detection with BTreeMap::range(..new_end) for O(log n + k) scan
  • Add buffered_bytes: usize to FlowDirection for O(1) buffer size queries
  • Remove update_memory() O(m×n) recomputation, replace with O(1) delta tracking at all 5 mutation sites
  • Add total_memory() public accessor for testability

Closes #11

Test plan

  • 10 new tests covering buffered_bytes insert/flush/overlap/dedup, range() boundary, total_memory tracking (insert/flush/finalize/RST/FIN-close)
  • All 85 tests pass (was 75 before this PR)
  • debug_assert_eq! in memory_used() verifies counter integrity on every call in test builds
  • debug_assert! guards on defensive insert paths
  • saturating_sub prevents usize underflow in release mode
  • clippy clean, fmt clean
  • 3-agent PR review (code, tests, silent-failures) — all findings addressed

Zious11 added 7 commits April 6, 2026 20:20
Design for issue #11 — BTreeMap::range() overlap detection and
incremental memory tracking in the TCP reassembly engine.
4-task plan for issue #11: buffered_bytes tracking, range-based
overlap detection, and incremental total_memory tracking.
- Use saturating_sub for insert delta to prevent usize underflow
- Add debug_assert! at both defensive insert guard sites
- Replace unwrap_or(0) with expect() at all 5 flow removal sites
- Add FIN-close total_memory test with buffered data
- Add buffered_bytes assertion to retransmission dedup test
- Add comments explaining pre-flush capture pattern
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes the TCP reassembly engine’s hot paths by avoiding repeated O(n) scans/summations and replacing them with incremental counters and bounded BTreeMap::range() overlap checks, with new tests documenting/guarding the intended behavior.

Changes:

  • Use BTreeMap::range(..new_end) to restrict overlap scans to only potentially overlapping segments.
  • Add FlowDirection::buffered_bytes and maintain it incrementally on insert/flush; use it for O(1) memory_used().
  • Remove global update_memory() recomputation and maintain TcpReassembler.total_memory via deltas, exposing total_memory() for tests.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/reassembly/segment.rs Switch overlap scan to range(..new_end); update buffered_bytes on segment inserts and flush removals.
src/reassembly/flow.rs Add buffered_bytes field and make memory_used() return it with a debug-time drift check.
src/reassembly/mod.rs Replace update_memory() with incremental total_memory tracking at insert/flush/removal sites; add total_memory() accessor.
tests/reassembly_segment_tests.rs Add assertions/tests for buffered_bytes behavior and range boundary overlap behavior.
tests/reassembly_flow_tests.rs Assert buffered_bytes initializes to 0.
tests/reassembly_engine_tests.rs Add/extend tests validating total_memory() is correct across RST/timeout/finalize/FIN-close scenarios.
docs/superpowers/specs/2026-04-06-reassembly-perf-design.md New design write-up for the perf changes (contains a couple of now-outdated statements).
docs/superpowers/plans/2026-04-06-reassembly-perf.md New implementation plan (contains a now-outdated note about total_memory visibility).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/superpowers/specs/2026-04-06-reassembly-perf-design.md
Comment thread docs/superpowers/plans/2026-04-06-reassembly-perf.md Outdated
Comment thread docs/superpowers/specs/2026-04-06-reassembly-perf-design.md Outdated
@Zious11 Zious11 merged commit 398e64a into develop Apr 7, 2026
4 checks passed
@Zious11 Zious11 deleted the feature/reassembly-perf branch April 7, 2026 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: use BTreeMap::range() and incremental memory tracking in reassembly

2 participants